//
//  KJStaticNormalCell.m
//  KJKit
//
//  Created by Tiger on 2022/6/9.
//

#import "KJStaticNormalCell.h"
#import <Masonry/Masonry.h>

@interface KJStaticNormalCell()

@property (nonatomic, strong) UILabel *descriptionLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *itemImageView;
@end

@implementation KJStaticNormalCell

@synthesize item = _item;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.itemImageView];
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}

- (void)cellWillAppear {
    [super cellWillAppear];
    
    self.titleLabel.text = self.item.titleText;
    self.detailTextLabel.text = self.item.detailText;
    self.itemImageView.image = self.item.image;
    self.descriptionLabel.text = self.item.descriptionText;
    if (!self.descriptionLabel.superview) {
        [self.contentView addSubview:self.descriptionLabel];
    }
    
    //styling
    self.titleLabel.font = self.item.titleFont;
    self.titleLabel.textColor = self.item.titleColor;
    self.detailTextLabel.font = self.item.detailFont;
    self.detailTextLabel.textColor = self.item.detailColor;
    self.descriptionLabel.font = self.item.descriptionFont;
    self.descriptionLabel.textColor = self.item.descriptionColor;
    
    CGFloat imageWidthSpace = 0;
    CGFloat descriptionSpace = 0;

    if (self.item.image) {
        imageWidthSpace = self.item.imageSize.width > 0 ? self.item.imageSize.width:self.itemImageView.image.size.width;
        imageWidthSpace += 15;//leading space
    }
    if (self.item.descriptionText) {
        descriptionSpace = 15/2;
    }
    
    if (self.item.titleText) {
        self.titleLabel.numberOfLines = self.item.numberOfLinesForTitle >= 0 ? self.item.numberOfLinesForTitle:1;
        self.titleLabel.lineBreakMode = self.item.lineBreakModeForTitle;
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (self.item.image) {
              make.leading.equalTo(self.itemImageView.mas_trailing).offset(15);
            } else {
              make.leading.equalTo(self.contentView.mas_leading).offset(15);
            }
            
            if (self.item.titleDescriptionRatio > 0) {
                make.width.equalTo(self.contentView.mas_width).multipliedBy(self.item.titleDescriptionRatio).offset(-30 - imageWidthSpace - descriptionSpace);
            } else {
                make.width.equalTo(self.contentView.mas_width).multipliedBy(.6).offset(-30 - imageWidthSpace - descriptionSpace);
            }
            if (self.item.detailText) {
                make.bottom.equalTo(self.contentView.mas_centerY);
            } else {
                make.centerY.equalTo(self.contentView.mas_centerY);
            }
        }];
    }

    if (self.item.detailText) {
//        self.detailTextLabel.textColor = [UIColor grayColor];
        if (@available(iOS 13.0, *)) {
            self.detailTextLabel.textColor = [UIColor systemGrayColor];
        } else {
            // Fallback on earlier versions
            self.detailTextLabel.textColor = [UIColor grayColor];
        }
        self.detailTextLabel.numberOfLines = self.item.numberOfLinesForDetail >= 0 ? self.item.numberOfLinesForDetail : 1;
        self.detailTextLabel.lineBreakMode = self.item.lineBreakModeForDetail;
        [self.detailTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView.mas_centerY);
            if (self.item.image) {
                make.leading.equalTo(self.itemImageView.mas_trailing).offset(15);
            } else {
                make.leading.equalTo(self.contentView.mas_trailing).offset(15);
            }
            if (self.item.titleDescriptionRatio > 0) {
                make.width.equalTo(self.contentView.mas_width).multipliedBy(self.item.titleDescriptionRatio).offset(-30 - imageWidthSpace - descriptionSpace);
            } else {
                make.width.equalTo(self.contentView.mas_width).multipliedBy(.6).offset(-30 - imageWidthSpace - descriptionSpace);
            }
        }];
    }

    if (self.item.descriptionText) {
        self.descriptionLabel.text = self.item.descriptionText;
        self.descriptionLabel.numberOfLines = self.item.numberOfLinesForDescription >= 0 ? self.item.numberOfLinesForDescription : 1;
        self.descriptionLabel.lineBreakMode = self.item.lineBreakModeForDescription;
        self.descriptionLabel.textAlignment = NSTextAlignmentRight;
       
        [self.descriptionLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (self.accessoryType == UITableViewCellAccessoryNone && !self.accessoryView) {
                make.trailing.equalTo(self.contentView.mas_trailing).offset(-15);
            } else {
                if (self.accessoryView) {
                    make.trailing.equalTo(self.contentView.mas_trailing).offset(-5);
                } else {
                    make.trailing.equalTo(self.contentView.mas_trailing);
                }
            }
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.leading.equalTo(self.titleLabel.mas_trailing).offset(self.item.titleDescriptionGap >= 0 ? self.item.titleDescriptionGap : 15);
        }];
    }
    
    if (self.item.image) {
        [self.itemImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.contentView).offset(15);
            make.centerY.equalTo(self.contentView.mas_centerY);
            if (!CGSizeEqualToSize(CGSizeZero, self.item.imageSize)) {
                make.width.equalTo(@(self.item.imageSize.width));
                make.height.equalTo(@(self.item.imageSize.height));
            } else {
                make.width.equalTo(@(self.item.image.size.width));
                make.height.equalTo(@(self.item.image.size.height));
            }
        }];
    }
}

- (UIImageView *)itemImageView {
    if (!_itemImageView) {
        _itemImageView = [[UIImageView alloc] init];
    }
    return _itemImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
    }
    return _titleLabel;
}

- (UILabel *)descriptionLabel {
    if (!_descriptionLabel) {
        _descriptionLabel = [UILabel new];
    }
    return _descriptionLabel;
}

@end
